package com.axend.aerosense.user.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.launcher.ARouter;
import com.axend.aerosense.base.bean.BaseCancelUserBean;
import com.axend.aerosense.base.bean.BaseStringBean;
import com.axend.aerosense.base.fragment.MvvmBaseFragment;
import com.axend.aerosense.base.storage.MmkvHelper;
import com.axend.aerosense.base.utils.LogUtils;
import com.axend.aerosense.base.viewmodel.BaseViewModel;
import com.axend.aerosense.common.UrlApi;
import com.axend.aerosense.common.bean.UserInfo;
import com.axend.aerosense.common.config.Gobal;
import com.axend.aerosense.common.services.ILoginService;
import com.axend.aerosense.common.services.config.ServicesConfig;
import com.axend.aerosense.common.ui.LoadingDialog;
import com.axend.aerosense.common.util.CheckUtil;
import com.axend.aerosense.common.util.JsonUtil;
import com.axend.aerosense.network.EasyHttp;
import com.axend.aerosense.network.cache.model.CacheMode;
import com.axend.aerosense.network.callback.ProgressDialogCallBack;
import com.axend.aerosense.network.exception.ApiException;
import com.axend.aerosense.user.R;
import com.axend.aerosense.user.UserSettingActivity;
import com.axend.aerosense.user.databinding.UserCancelNumberVerificationFragmentBinding;
import com.blankj.utilcode.util.ToastUtils;

public class UserCancelNumberVerificationFragment extends MvvmBaseFragment<UserCancelNumberVerificationFragmentBinding, BaseViewModel> {
    private UserInfo userInfo;
    @Autowired(name = ServicesConfig.User.LONGING_SERVICE)
    ILoginService iLoginService;

    public static UserCancelNumberVerificationFragment getInstance() {
        UserCancelNumberVerificationFragment fragment = new UserCancelNumberVerificationFragment();
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ARouter.getInstance().inject(this);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ((UserSettingActivity) getActivity()).setTitle("");
        init();
    }

    private void init() {
        userInfo = MmkvHelper.getInstance().getMmkv().decodeParcelable(Gobal.SAVE_KEY.USR_INFO, UserInfo.class);
        viewDataBinding.userButton.setOnClickListener(v -> {
            if (TextUtils.isEmpty(viewDataBinding.userEtCode.getText().toString())) {
                ToastUtils.showShort(getString(R.string.common_input_password));
            } else {
                onDeleteUser(getActivity(), viewDataBinding.userEtCode.getText().toString());
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.user_cancel_number_verification_fragment;
    }

    @Override
    public int getBindingVariable() {
        return 0;
    }

    @Override
    protected BaseViewModel getViewModel() {
        return null;
    }

    @Override
    protected void onRetryBtnClick() {

    }

    //注销用户
    private void onDeleteUser(Activity activity, String code) {
        if(!CheckUtil.isPasswordOk(code)){
            ToastUtils.showShort(getString(R.string.common_check_password));
            return;
        }
        BaseCancelUserBean base = new BaseCancelUserBean(code);
        EasyHttp.post(UrlApi.URL.CancelUser.getUrl())
                .headers("token", MmkvHelper.getInstance().getMmkv().decodeString(Gobal.SAVE_KEY.TOKEN))
                .cacheMode(CacheMode.NO_CACHE)
                .upJson(JsonUtil.toJson(base))
                .execute(new  ProgressDialogCallBack<BaseStringBean>(LoadingDialog.getProgressDialog(activity))  {
                    @Override
                    public void onError(ApiException e) {
                        dismissProgress();
                        //失败
                        ToastUtils.showShort(e.getMessage());
                    }

                    @Override
                    public void onSuccess(BaseStringBean baseStringBean) {
                        dismissProgress();
                        //注销成功
                        iLoginService.logout(getActivity(), () -> {
                            //这个会导致跳转到上一个界面，如果上一个界面是桌面，那么会回到桌面
                            iLoginService.login();
                            activity.finish();
                        });


                    }

                    @Override
                    public void onTokenExpire() {
                        dismissProgress();
                        //Token错误
                        LogUtils.d("MyTAG", "Token错误！！！");
                    }
                });
    }


}
